﻿using System;
using System.Threading;

namespace FastNet.Internal
{
	internal struct ReadLock : IDisposable
	{
		public ReadLock( ReaderWriterLockSlim @lock )
		{
			m_lock = @lock;

			if ( m_lock != null )
			{
				m_lock.EnterReadLock();
			}
		}


		public void Dispose()
		{
			if ( m_lock != null )
			{
				m_lock.ExitReadLock();

				m_lock = null;
			}
		}


		private ReaderWriterLockSlim m_lock;
	}


	internal struct WriteLock : IDisposable
	{
		public WriteLock( ReaderWriterLockSlim @lock )
		{
			m_lock = @lock;

			if ( m_lock != null )
			{
				m_lock.EnterWriteLock();
			}
		}


		public void Dispose()
		{
			if ( m_lock != null )
			{
				m_lock.ExitWriteLock();

				m_lock = null;
			}
		}


		private ReaderWriterLockSlim m_lock;
	}
}
